<?php

namespace Common\Model;

use Think\Model;

class VUserNoticeModel extends Model
{
    public function get($id)
    {
        $result = $this->where('id="%d"', $id)->find();
        return $result;
    }

    public function read($id)
    {
        $data['status'] = 1;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function get_list($user_id, $search_key, $status, $page_no = 1, $page_size = 0)
    {
        $condition = $this->assembleCondition($user_id, $search_key, $status);
        $count = 0;

        if ($page_size > 0) {
            $list = $this->where($condition)->order('id desc')->limit(($page_no - 1) * $page_size, $page_size)->select();
            $count = $this->where($condition)->count();
        } else {
            $list = $this->where($condition)->order('id desc')->select();
        }
        return [$list, $count];
    }

    private function assembleCondition($user_id, $search_key, $status)
    {
        $condition = array();
        if (!empty($user_id)) {
            $condition['user_id'] = array('eq', $user_id);
        }
        if (!empty($search_key)) {
            $condition['title'] = array('like', '%' . $search_key . '%');
        }

        if ($status != "" && $status != null) {
            $condition['status'] = array('eq', $status);
        } else {
            $condition['status'] = array('egt', 0);
        }
        $condition['notice_status'] = array('egt', 0);
        return $condition;
    }
}